package com.dimong.www.loan.ui.adapter.holder;


import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.dimong.loan.sdk.ui.adapter.BaseRecyclerAdapter;
import com.dimong.loan.sdk.ui.adapter.BaseRecyclerViewHolder;
import com.dimong.www.loan.R;
import com.dimong.www.loan.bean.ClassifyBean;
import com.dimong.www.loan.ui.adapter.main.LoadRecommendItemAdapter;
import com.dimong.www.loan.widget.RecyclerLayoutManager;

import java.util.List;

public class LoadRecommendHolder extends BaseRecyclerViewHolder {
    private RecyclerView recycler_item_loan_recommend;
    private RecyclerLayoutManager recyclerLayoutManager;
    private LoadRecommendItemAdapter adapter;

    private LoadRecommendHolderInterface loadRecommendHolderInterface;
    public LoadRecommendHolder(Context context, ViewGroup parent,LoadRecommendHolderInterface loadRecommendHolderInterface) {
        super(context, R.layout.item_loan_recommend, parent);

        recycler_item_loan_recommend = itemView.findViewById(R.id.recycler_item_loan_recommend);
        recyclerLayoutManager = new RecyclerLayoutManager(context, 4, LinearLayoutManager.VERTICAL, false);
        recycler_item_loan_recommend.setLayoutManager(recyclerLayoutManager);
        recycler_item_loan_recommend.setItemAnimator(null);
        adapter = new LoadRecommendItemAdapter();
        recycler_item_loan_recommend.setAdapter(adapter);

        this.loadRecommendHolderInterface = loadRecommendHolderInterface;
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                if(LoadRecommendHolder.this.loadRecommendHolderInterface!=null){
                    LoadRecommendHolder.this.loadRecommendHolderInterface.onLoadRecommendHolderClick(getLayoutPosition(),position);
                }
            }
        });
    }

    public void setLoadRecommendHolderInterface(LoadRecommendHolderInterface loadRecommendHolderInterface) {
        this.loadRecommendHolderInterface = loadRecommendHolderInterface;
    }

    @Override
    public void setData(Object object) {
        super.setData(object);
        if (object instanceof List) {
            adapter.clear();
            List<ClassifyBean> classify = (List<ClassifyBean>) object;
            if (classify != null) {
                for (int i = 0; i < classify.size(); i++) {
                    adapter.addData(classify.get(i));
                }
                adapter.updateChange();
            }
        }
    }

    public interface LoadRecommendHolderInterface {
        void onLoadRecommendHolderClick(int adapterPos, int pos);
    }
}
